package com.rong.DAO;

import com.rong.hibernate.BaseHibernateDAO;
import com.rong.hibernate.HibernateSessionFactory;

import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.rong.ORM.ZynUser;
/**
 * A data access object (DAO) providing persistence and search support for
 * ZynUser entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.rong.DAO.ZynUser
 * @author MyEclipse Persistence Tools
 */

public class ZynUserDAO extends BaseHibernateDAO {
	HibernateSessionFactory util = new HibernateSessionFactory();
	private static final Log log = LogFactory.getLog(ZynUserDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String PWD = "pwd";
	public static final String EMAIL = "email";
	public static final String NAME_ZH = "nameZh";
	public static final String POSITION = "position";
	public static final String COM_ID = "comId";
	public static final String EXTATTR1 = "extattr1";
	public static final String EXTATTR2 = "extattr2";
	public static final String EXTATTR3 = "extattr3";
	public static final String EXTATTR4 = "extattr4";
	public static final String EXTATTR5 = "extattr5";
	public static final String EXTATTR6 = "extattr6";
	public static final String EXTATTR7 = "extattr7";
	public static final String EXTATTR8 = "extattr8";
	public static final String EXTATTR9 = "extattr9";

	public void save(ZynUser transientInstance) {
		log.debug("saving ZynUser instance");
		try {
			Transaction ts=null;
			ts=getSession().beginTransaction();
			getSession().save(transientInstance);
			ts.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ZynUser persistentInstance) {
		log.debug("deleting ZynUser instance");
		try {
			Transaction ts=null;
			ts=getSession().beginTransaction();
			getSession().delete(persistentInstance);
			ts.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZynUser findById(java.lang.Integer id) {
		log.debug("getting ZynUser instance with id: " + id);
		try {
			ZynUser instance = (ZynUser) getSession().get(
					ZynUser.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZynUser instance) {
		log.debug("finding ZynUser instance by example");
		try {
			List results = getSession().createCriteria("com.rong.DAO.ZynUser")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZynUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ZynUser as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByPwd(Object pwd) {
		return findByProperty(PWD, pwd);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByNameZh(Object nameZh) {
		return findByProperty(NAME_ZH, nameZh);
	}

	public List findByPosition(Object position) {
		return findByProperty(POSITION, position);
	}

	public List findByComId(Object comId) {
		return findByProperty(COM_ID, comId);
	}

	public List findByExtattr1(Object extattr1) {
		return findByProperty(EXTATTR1, extattr1);
	}

	public List findByExtattr2(Object extattr2) {
		return findByProperty(EXTATTR2, extattr2);
	}

	public List findByExtattr3(Object extattr3) {
		return findByProperty(EXTATTR3, extattr3);
	}

	public List findByExtattr4(Object extattr4) {
		return findByProperty(EXTATTR4, extattr4);
	}

	public List findByExtattr5(Object extattr5) {
		return findByProperty(EXTATTR5, extattr5);
	}

	public List findByExtattr6(Object extattr6) {
		return findByProperty(EXTATTR6, extattr6);
	}

	public List findByExtattr7(Object extattr7) {
		return findByProperty(EXTATTR7, extattr7);
	}

	public List findByExtattr8(Object extattr8) {
		return findByProperty(EXTATTR8, extattr8);
	}

	public List findByExtattr9(Object extattr9) {
		return findByProperty(EXTATTR9, extattr9);
	}

	public List findAll() {
		log.debug("finding all ZynUser instances");
		try {
			String queryString = "from ZynUser";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZynUser merge(ZynUser detachedInstance) {
		log.debug("merging ZynUser instance");
		try {
			Transaction ts=null;
			ts=getSession().beginTransaction();
			ZynUser result = (ZynUser) getSession().merge(detachedInstance);
			ts.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZynUser instance) {
		log.debug("attaching dirty ZynUser instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZynUser instance) {
		log.debug("attaching clean ZynUser instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	 public int isExist(String name,String password){
	        //int state = 0 ;        //��ʼ��״̬��
	        Session session = util.getSession();
	        try{ 
	            Query query = session.createQuery("from ZynUser u where u.name = ?");
	            query.setString(0, name);
	            List list = query.list();
	            if(null == list || 0 == list.size()){
	                return -1 ;     //�û������
	            }
	            Query query2 = session.createQuery("from ZynUser u where u.name = ? and u.pwd = ?");
	            query2.setString(0, name);
	            query2.setString(1, password);
	            List list2 = query2.list();
	            if(null == list2){
	                return -2 ;        //���벻��ȷ
	            }
	            Iterator it = list2.iterator();
	            ZynUser user = (ZynUser)it.next();
	            return user.getId();    //��֤�ɹ�,ȡIDֵ
	           
	        }catch(Exception e){
	            System.out.println("UserDaoImpl.isExist()���������쳣:");
	            e.printStackTrace();
	            return 0;    //�쳣ʱ����0
	        }finally{
	            util.closeSession();
	        }
	    }
}